package com.lw.smartdevice.controller;

import com.lw.smartdevice.dto.R;
import com.lw.smartdevice.pojo.SysDevice;
import com.lw.smartdevice.service.SysDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Administrator
 * @version 1.0
 * @description:
 * @date 2022/3/22 17:35
 */
@RestController
@RequestMapping("/devices")
public class DeviceController {

    private SysDeviceService deviceService;

    @Autowired
    public void setDeviceService(SysDeviceService deviceService) {
        this.deviceService = deviceService;
    }

    @PostMapping("/add")
    public R addDevice(@RequestBody SysDevice device){
        deviceService.addDevice(device);
        return R.setOK();
    }

    @GetMapping("/devices/{deviceId}")
    public R findById(@PathVariable String deviceId){
        return R.setOK(deviceService.findById(deviceId));
    }

    @GetMapping("/devices")
    public R findAll(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "1") int limit,SysDevice sysDevice){
        return R.setOK(deviceService.findAll(page,limit,sysDevice));
    }

    @PostMapping("/command/{deviceId}/{command}")
    public R sendControl(@PathVariable String deviceId,@PathVariable String command){
        System.err.println("收到了向设备发送命令的操作："+deviceId+"--->"+command);
        deviceService.sendControl(deviceId,command);
        return R.setOK();
    }
}
